package t2h.activities;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;

import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;
import javax.microedition.rms.RecordStoreFullException;
import javax.microedition.rms.RecordStoreNotFoundException;
import javax.microedition.rms.RecordStoreNotOpenException;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import com.sun.lwuit.list.DefaultListModel;
import com.sun.lwuit.list.ListModel;

import t2h.valueobjects.SongVO;

public class ReadXmlRecord extends DefaultHandler {

       private SongVO acct;
       private String temp;
       //private ArrayList accList = new ArrayList<SongVO>();
       //ListModel accList = new DefaultListModel();
       
       private RecordStore recordID = null;
       private RecordStore recordName = null;
       private RecordStore recordAuthor = null;
       private RecordStore recordLyric = null;
       

       //** The main method sets things up for parsing *//*
       public ReadXmlRecord() throws IOException, SAXException,
                     ParserConfigurationException {
             
              //Create a "parser factory" for creating SAX parsers
              SAXParserFactory spfac = SAXParserFactory.newInstance();

              //Now use the parser factory to create a SAXParser object
              SAXParser sp = spfac.newSAXParser();

              //Create an instance of this class; it defines all the handler methods
              ReadXmlFile handler = new ReadXmlFile();

              //Finally, tell the parser to parse the input and notify the handler
              //String fileName = "/Vol471.xml";
              String fileName = "/file2.xml";
              //File fXMLFile = new File(fileName);
              InputStream inputStream= this.getClass().getResourceAsStream(fileName);
              Reader reader = new InputStreamReader(inputStream,"UTF-8");
          	 
              InputSource is = new InputSource(reader);
              is.setEncoding("UTF-8");
              sp.parse(is,handler);
             
             // return handler.getaccListKara();
       }    
       
       public void characters(char[] buffer, int start, int length) {
              temp = new String(buffer, start, length);
       }
         
       public void startElement(String uri, String localName,
                     String qName, Attributes attributes) throws SAXException {
              temp = "";
              if (qName.equalsIgnoreCase("song")) {
                     acct = new SongVO();
                     acct.setKaraokeType(attributes.getValue("type"));

              }
       }
       
       public RecordStore getaRecordID(){
    	   return recordID;
       }
       
       public RecordStore getaRecordName(){
    	   return recordName;
       }
       
       public RecordStore getaRecordAuthor(){
    	   return recordAuthor;
       }
       
       public RecordStore getaRecordLyric(){
    	   return recordLyric;
       }
       
       private void OpenRecords() throws RecordStoreFullException, RecordStoreNotFoundException, RecordStoreException{
    	   recordID = RecordStore.openRecordStore("recordID", true);
		   recordName = RecordStore.openRecordStore("recordName", true);
		   recordAuthor = RecordStore.openRecordStore("recordAuthor", true);
		   recordLyric = RecordStore.openRecordStore("recordLyric", true);
       }

       public void endElement(String uri, String localName, String qName)
                     throws SAXException {
	    	   try {
				OpenRecords();
				} catch (RecordStoreFullException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (RecordStoreNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (RecordStoreException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
	    	   
	    	   if (qName.equalsIgnoreCase("name")) {
                     //acct.setName(temp);
	    		   byte[] byteOutputDataName = temp.getBytes();
	 		    	try {
						recordName.addRecord(byteOutputDataName, 0, byteOutputDataName.length);
					} catch (RecordStoreNotOpenException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (RecordStoreFullException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (RecordStoreException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
              } else if (qName.equalsIgnoreCase("id")) {
            	  byte[] byteOutputDataID = temp.getBytes();
	 		    	try {
						recordID.addRecord(byteOutputDataID, 0, byteOutputDataID.length);
					} catch (RecordStoreNotOpenException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (RecordStoreFullException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (RecordStoreException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
              } else if (qName.equalsIgnoreCase("author")) {
            	  byte[] byteOutputDataAuthor = temp.getBytes();
	 		    	try {
						recordAuthor.addRecord(byteOutputDataAuthor, 0, byteOutputDataAuthor.length);
					} catch (RecordStoreNotOpenException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (RecordStoreFullException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (RecordStoreException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
              }else if (qName.equalsIgnoreCase("lyric")) {
            	  byte[] byteOutputDataLyric = temp.getBytes();
	 		    	try {
						recordLyric.addRecord(byteOutputDataLyric, 0, byteOutputDataLyric.length);
					} catch (RecordStoreNotOpenException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (RecordStoreFullException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (RecordStoreException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
	          }
       }
       
       
}